﻿#include "card.h"

#define CARD_SIZE_WEIGHT 125
#define CARD_SIZE_HEIGHT 150
#define CARD_PAINTER_OFFSET 5
#define CARD_MARGIN 16
#define CELL_WIDTH 136
#define CELL_HEIGHT 160

#include <QPainter>
#include <QSequentialAnimationGroup>
#include <QPen>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsItem>
#include <QKeyEvent>


Card::Card(QGraphicsScene *scene, QString strFace, Group group, Monster_Type type):
    m_scene(scene),
    m_isBack(true),
    m_isSelect(false),
    m_isStadingBy(true),
    m_group(group),
    m_type(type),
    m_upAllow(true),
    m_downAllow(true),
    m_leftAllow(true),
    m_rightAllow(true)
{
    m_scene->addItem(this);
    //this->setPos(525, 425); // 默认位置
    m_destion = QPoint(pos().x(), pos().y());
    this->setFlag(QGraphicsItem::ItemIsFocusable);
    //this->setFlag(QGraphicsItem::ItemIsMovable);
    setAcceptHoverEvents(true);

    initAnimation();
    drawFace(strFace);
    setCacheMode(DeviceCoordinateCache);


}

Card::~Card()
{

}

QRectF Card::boundingRect() const
{
    return QRectF(-CARD_SIZE_WEIGHT / 2, -CARD_SIZE_WEIGHT / 2,
                  CARD_SIZE_WEIGHT, CARD_SIZE_HEIGHT);

    // 保证图片缩放期间以原点为中心
    //return QRectF(0, 0, CARD_SIZE_WEIGHT, CARD_SIZE_HEIGHT);
}

bool Card::checkColliding()
{
    if(collidingItems().isEmpty()){
        return false;
    }
    return true;
}

void Card::setIsBack(bool para)
{
    m_isBack = para;
}

void Card::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    // 镇压宏
    Q_UNUSED(option);
    Q_UNUSED(widget)

    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
    if(m_isBack){
        painter->drawImage(boundingRect(),
                           QImage(":/Monster_Kill/Res/Img/Card2.png"));
    }else{
        painter->drawImage(boundingRect(), m_face->toImage());
    }
}

void Card::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    Q_UNUSED(event); // 镇压警告

    if(m_isBack && m_isSelect){
        openAnimation();
    }
}

void Card::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    Q_UNUSED(event); // 镇压警告
    m_isSelect = true;
    m_scene->setFocusItem(this);
    Card_Select->start();
}

void Card::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
    Q_UNUSED(event); // 镇压警告
    m_isSelect = true;
    m_scene->setFocusItem(this);
    Card_Select->start();
}

void Card::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    Q_UNUSED(event); // 镇压警告
    m_isSelect = false;
    setProperty("scale", 1);
    Card_Select->stop();
}

void Card::keyPressEvent(QKeyEvent *event)
{
    Card_Select->stop();
    this->setScale(1);

    if(m_isSelect){
        switch (event->key()){
        case Qt::Key_W:
            m_isSelect = false;
            upAnimation();
            break;
        case Qt::Key_S:
            m_isSelect = false;
            downAnimation();
            break;
        case Qt::Key_A:
            m_isSelect = false;
            leftAnimation();
            break;
        case Qt::Key_D:
            m_isSelect = false;
            rightAnimation();
            break;
        default:
            break;
        }
    }

    event->accept();
}

void Card::initAnimation()
{
    // 加载动画链表 0向上 1向下 2向左 3向右
    m_upAnimations = new QPropertyAnimation(this, "pos");
    m_upAnimations->setDuration(1000); // 设置动画持续的时长
    m_upAnimations->setEasingCurve(QEasingCurve::InOutBack);
    connect(m_upAnimations, &QPropertyAnimation::finished,[this](){
        m_isStadingBy = true;
    });

    m_downAnimations = new QPropertyAnimation(this, "pos");
    m_downAnimations->setDuration(1000); // 设置动画持续的时长
    m_downAnimations->setEasingCurve(QEasingCurve::InOutBack);
    connect(m_downAnimations, &QPropertyAnimation::finished,[this](){
        m_isStadingBy = true;
    });

    m_leftAnimations = new QPropertyAnimation(this, "pos");
    m_leftAnimations->setDuration(1000); // 设置动画持续的时长
    m_leftAnimations->setEasingCurve(QEasingCurve::InOutBack);
    connect(m_leftAnimations, &QPropertyAnimation::finished,[this](){
        m_isStadingBy = true;
    });

    m_rightAnimations = new QPropertyAnimation(this, "pos");
    m_rightAnimations->setDuration(1000); // 设置动画持续的时长
    m_rightAnimations->setEasingCurve(QEasingCurve::InOutBack);
    connect(m_rightAnimations, &QPropertyAnimation::finished,[this](){
        m_isStadingBy = true;
    });


    // 启用连续动画组
    Card_Open = new QSequentialAnimationGroup(this);
    Card_Select = new QSequentialAnimationGroup(this);

    // 制作卡牌旋转动画
    m_rotAnimation = new QPropertyAnimation(this, "rotation");
    m_rotAnimation->setDuration(83);
    m_rotAnimation->setLoopCount(2);
    m_rotAnimation->setEndValue(360);
    m_rotAnimation->setEasingCurve(QEasingCurve::InOutBack);

    // 制作卡牌消失动画
    m_fadeAnimation = new QPropertyAnimation(this, "opacity", Card_Open);
    m_fadeAnimation->setLoopCount(3);
    m_fadeAnimation->setDuration(139);
    m_fadeAnimation->setEndValue(0.6);
    m_fadeAnimation->setEasingCurve(QEasingCurve::InOutBack);
    //Card_Open->addPause(50);
    connect(m_fadeAnimation, &QPropertyAnimation::finished, [this](){
        m_isBack = false;
        update();
    });

    // 制作卡牌出现动画
    m_openAnimation = new QPropertyAnimation(this, "opacity", Card_Open);
    m_openAnimation->setDuration(637);
    m_openAnimation->setEndValue(1);
    m_openAnimation->setEasingCurve(QEasingCurve::InOutBack);

    // 制作选中动画
    QPropertyAnimation* m_BigAnimation = new QPropertyAnimation(this, "scale", Card_Select);
    m_BigAnimation->setDuration(500);
    m_BigAnimation->setEndValue(1.05);
    m_BigAnimation->setEasingCurve(QEasingCurve::Linear);

    QPropertyAnimation* m_SimAnimation = new QPropertyAnimation(this, "scale", Card_Select);
    m_SimAnimation->setDuration(500);
    m_SimAnimation->setEndValue(1);
    m_SimAnimation->setEasingCurve(QEasingCurve::Linear);
    Card_Select->setLoopCount(-1);


}

void Card::drawFace(QString strFace)
{
    // 绘制卡牌的牌面
    m_face = new QPixmap(CARD_SIZE_WEIGHT, CARD_SIZE_HEIGHT);
    m_face->fill(Qt::transparent); // 初始化为透明，以便后续绘制红色背景

    QPainter painter(m_face);
    painter.setRenderHint(QPainter::Antialiasing); // 抗锯齿，使绘制更平滑

    // 设置灰色边框
    QLinearGradient grad1(QPoint(0, 0), QPoint(CARD_SIZE_WEIGHT, CARD_SIZE_HEIGHT));
    grad1.setColorAt(1, Qt::darkGray);
    grad1.setColorAt(0, Qt::lightGray);
    painter.setBrush(QBrush(grad1));
    painter.drawRect(QRectF(0, 0, CARD_SIZE_WEIGHT, CARD_SIZE_HEIGHT));

    // 设置红色/蓝色背景
    QLinearGradient grad2(QPoint(CARD_PAINTER_OFFSET, CARD_PAINTER_OFFSET),
                          QPoint(CARD_SIZE_WEIGHT - CARD_PAINTER_OFFSET,
                                 CARD_SIZE_HEIGHT - CARD_PAINTER_OFFSET));
    grad2.setColorAt(0, m_group == RED ? QColor(255, 140, 105) : QColor(155, 200, 200)); // 浅红色 - 天蓝色
    grad2.setColorAt(1, m_group == RED ? QColor(139, 5, 15) : QColor(6, 82, 121)); // 玫瑰红 - 靛蓝色
    QBrush brush(grad2);
    painter.setBrush(brush);
    painter.drawRect(QRectF(CARD_PAINTER_OFFSET, CARD_PAINTER_OFFSET,
                            CARD_SIZE_WEIGHT - CARD_PAINTER_OFFSET * 2,
                            CARD_SIZE_HEIGHT - CARD_PAINTER_OFFSET * 2)); // 绘制红色矩形填充整个QPixmap

    // 绘制图片
    painter.drawPixmap(CARD_PAINTER_OFFSET, CARD_SIZE_HEIGHT/4,
            QPixmap(strFace).scaled(QSize(CARD_SIZE_WEIGHT - CARD_PAINTER_OFFSET * 2,
                                          CARD_SIZE_HEIGHT),
                            Qt::KeepAspectRatio, Qt::SmoothTransformation)); // 在(0,0)位置绘制图片
    painter.end(); // 结束绘制
}

void Card::upAnimation()
{
    if(m_isBack || !m_isStadingBy || !m_upAllow){
        return;
    }
    m_isStadingBy = false;
    m_upAnimations->setEndValue(QPoint(pos().x(), pos().y() - CELL_HEIGHT - CARD_MARGIN));
    m_upAnimations->start();
    emit cardDO(m_group, 1, this);

}

void Card::downAnimation()
{
    if(m_isBack || !m_isStadingBy || !m_downAllow){
        return;
    }
    m_isStadingBy = false;
    m_downAnimations->setEndValue(QPoint(pos().x(), pos().y() + CELL_HEIGHT + CARD_MARGIN));
    m_downAnimations->start();
    emit cardDO(m_group, 2, this);
}

void Card::leftAnimation()
{
    if(m_isBack || !m_isStadingBy || !m_leftAllow){
        return;
    }
    m_isStadingBy = false;
    m_leftAnimations->setEndValue(QPoint(pos().x() - CELL_WIDTH - CARD_MARGIN, pos().y()));
    m_leftAnimations->start();
    emit cardDO(m_group, 3, this);
}

void Card::rightAnimation()
{
    if(m_isBack || !m_isStadingBy || !m_rightAllow){
        return;
    }
    m_isStadingBy = false;
    m_rightAnimations->setEndValue(QPoint(pos().x() + CELL_WIDTH + CARD_MARGIN, pos().y()));
    m_rightAnimations->start();
    emit cardDO(m_group, 4, this);
}

void Card::openAnimation()
{
    Card_Open->start();
    emit cardDO(m_group, 0, this);
}
